using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SqlTools.CodeGenerator.UI.Controls
{
	public partial class FolderData : UserControl
	{
		public event EventHandler DirectoryChanged;

		public FolderData()
		{
			InitializeComponent();
		}

		public Environment.SpecialFolder RootFolder
		{
			get { return folderBrowserDialog.RootFolder; }
			set { folderBrowserDialog.RootFolder = value; }
		}

		public string SelectedPath
		{
			get { return txtPath.Text; }
			set 
			{
				if ( txtPath.Text != value )
				{
					txtPath.Text = value;
					OnDirectoryChanged();
				}
			}
		}

		public bool ShowNewFolderButton
		{
			get { return folderBrowserDialog.ShowNewFolderButton; }
			set { folderBrowserDialog.ShowNewFolderButton = value; }
		}

		private void btnBrowse_Click(object sender, EventArgs e)
		{
			if ( Directory.Exists(txtPath.Text) )
			{
				folderBrowserDialog.SelectedPath = txtPath.Text;
			}
			else
			{
				folderBrowserDialog.SelectedPath = "";
			}

			if ( folderBrowserDialog.ShowDialog() == DialogResult.OK )
			{
				txtPath.Text = folderBrowserDialog.SelectedPath;
				OnDirectoryChanged();
			}
		}

		protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
		{
			base.SetBoundsCore(x, y, width, 23, specified);
		}


		protected void OnDirectoryChanged()
		{
			if ( DirectoryChanged != null )
			{
				DirectoryChanged(this, EventArgs.Empty);
			}
		}

		private void txtPath_Leave(object sender, EventArgs e)
		{
			OnDirectoryChanged();
		}
	}
}
